<?php

namespace App\Http\Controllers\Backend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\PermissionService;

class PermissionController extends Controller
{

    private $permissionService;

    public function __construct(PermissionService $permissionService)
    {

        $this->permissionService = $permissionService;

    }

    public function index()
    {
        $permissions = $this->permissionService->lists();
        return view('backend.permission.index', compact('permissions'));
    }


    public function create()
    {
        return view('backend.permission.create');
    }

    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required'
        ]);
        if($this->permissionService->storePermisson($request->except('_token'))){
            flash('添加成功!', 'success');
            return redirect()->route('admin.permission.index');
        }
        
    }

    public function edit($id)
    {

        return view('backend.permission.edit')->with('permission', $this->permissionService->find($id));
      
    }

    public function update($id, Request $request)
    {
        if($this->permissionService->updatePermission($id, $request->except(['_token', '_method']))){
            flash('修改成功!', 'success');
            return redirect()->route('admin.permission.index');
        }
    }

    public function destroy($id)
    {
        if($this->permissionService->deletePermission($id)){
           return ['status' => 200, 'msg' => 'ok'];
        }
        return 'error';
    }
}
